function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {
        //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
//					document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            var json=JSON.parse(xmlhttp.responseText);

            json=json.data.reverse();

            for(let i = 0;i<json.length;i++){

                var li=document.createElement("li");

                var spanName=document.createElement("span");
                spanName.innerHTML=json[i].studentName;
                var spansex=document.createElement("span");
                spansex.innerHTML=json[i].sex;
                var spanage=document.createElement("span");
                spanage.innerHTML=json[i].age;

                li.appendChild(spanName);
                li.appendChild(spansex);
                li.appendChild(spanage);
                document.querySelector("#list").appendChild(li);
            }
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
    xmlhttp.send();
}

loadXMLDoc();


document.querySelector("#btn").addEventListener("click",function(){
    // 获取姓名
    var n =document.querySelector("#n").value;
    // 获取性别
    var s =document.querySelector("#s").value;
    // 获取年龄
    var a =document.querySelector("#a").value;

    if(n != "" && a != ""){
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // IE6, IE5 浏览器执行代码
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.querySelector("#list").innerHTML="";
                loadXMLDoc()
            }
        }
        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+n+"&sex="+s+"&age="+a,true);
        // xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+n+"&sex="+s+"&age="+a,true);
        xmlhttp.send();
    }else {
        alert("不可为空")
    }
    // ajax代码


})